using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace ProjektZespolowy
{
    /// <summary>
    /// Gets User Picture.
    /// If you want display user picture in image scr write GetUserPicture.aspx?id=user_id
    /// </summary>
    public partial class GetUserPicture : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                string strImageID = Request["id"];

                // Create Instance of Connection and Command Object
                SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString);
                SqlCommand myCommand = new SqlCommand("Select avatar, avatar_mime from [user] Where id=" + strImageID, myConnection);

                try
                {
                    myConnection.Open();                    
                    SqlDataReader myDataReader;
                    myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

                    while ((myDataReader.Read()) && myDataReader["avatar_mime"].ToString().Length!=0)
                    {
                        Response.ContentType = myDataReader["avatar_mime"].ToString();
                        Response.BinaryWrite((byte[])myDataReader["avatar"]);
                    }
                    myConnection.Close();
                }
                catch (SqlException) 
                {

                }
            }
        }
    }
}